<?php
namespace Home\Controller;
class AdminController extends CommonController {

	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
    	$this->Is_power();        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
	}

// 0普通  1超级管理员   3市场  4运营
	//管理员列表
	public function admin_list()
	{
		$db_admin = M("admin");
		$adminlist = $db_admin->where(array("isdelete"=>0))->select();
		foreach ($adminlist as $key => &$value) {
			$value['powerlist'] = str_replace("member","主播",$value['powerlist']);
			$value['powerlist'] = str_replace("audit","审核",$value['powerlist']);
			$value['powerlist'] = str_replace("media","自媒体",$value['powerlist']);
			$value['powerlist'] = str_replace("bgm","背景音",$value['powerlist']);
			$value['powerlist'] = str_replace("sign","签约",$value['powerlist']);
			$value['powerlist'] = str_replace("show","展示",$value['powerlist']);
			$value['powerlist'] = str_replace("send","值班主播",$value['powerlist']);
			$value['powerlist'] = str_replace("order","订单",$value['powerlist']);
			$value['powerlist'] = str_replace("message","消息",$value['powerlist']);
			$value['powerlist'] = str_replace("money","财务",$value['powerlist']);
			$value['powerlist'] = str_replace("work","工单",$value['powerlist']);
			$value['powerlist'] = str_replace("audio","音频",$value['powerlist']);
			$value['powerlist'] = str_replace("add_del","加钱",$value['powerlist']);
			$value['powerlist'] = str_replace("inside","内部",$value['powerlist']);
			$value['powerlist'] = str_replace("system","系统",$value['powerlist']);
		}
		// p($adminlist);exit();
		$this->assign('adminlist',$adminlist);
		$this->display();
	}

	//创建管理员
	public function admin_add()
	{
		$this->display();
	}

	public function add_admin()
	{
		// p($_POST);exit();
		$username = I('post.username');
		$password = I('post.password');
		$founder = I('post.founder');
		$sex = I('post.sex');
		$powerlist = !empty(I('post.power_list'))?implode(',',I('post.power_list')):'';
		if (empty($username)||empty($password)||empty($powerlist)) {
			$this->error('参数错误',"",1);
		}
		$admin = D("admin");
		if (!$admin->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $admin->create(); // 保存生成的数据对象
			$data['heard_adminname'] = $username;
			$data['founder'] = $founder;
			$data['password'] = md5($password);
			$data['powerlist'] = $powerlist;
			$data['sex'] = $sex;
//			 p($data);exit();
			$result = $admin->add($data);
		$this->adminlog("添加了管理员".$result);
			$this->success('创建成功',"/Home/Admin/admin_list",1);
		}

	}


	//修改管理员
	public function admin_edit()
	{
		$id = I('get.id');
		$db_admin = M("admin");
		$admindata = $db_admin->where(array('heard_admin_id'=>$id))->find();
		// p($admindata);exit();
		$this->assign('admindata',$admindata);
		$this->display();
	}

	public function edit_admin()
	{
		$id = I('post.id');
		$username = I('post.username');
		$password = I('post.password');
		$founder = I('post.founder');
		$sex = I('post.sex');
		$powerlist = !empty(I('post.power_list'))?implode(',',I('post.power_list')):'';
		if (empty($username)||empty($powerlist)) {
			$this->error('参数错误',"",1);
		}

		$admin = D("admin");
		if (!$admin->create()){//自动验证
			$this->error('参数错误',"",1);
		}else{
			$data = $admin->create(); // 保存生成的数据对象
			$data['heard_adminname'] = $username;
			$data['password'] = md5($password);
			$data['founder'] = $founder;
			$data['powerlist'] = $powerlist;
			$data['sex'] = $sex;
			if (empty($password)) {
				unset($data['password']);
			}
			// p($data);exit();
			$admin->where(array('heard_admin_id'=>$id))->save($data);//更新数据
			// echo $admin->_sql();exit();
			$this->adminlog("修改了管理员".$id);
			$this->success('修改成功',"/Home/Admin/admin_list",1);
		}
	}
	//管理员操作记录列表
	public function adminlog_list()
	{
		$db_adminlog = M("adminlog l");
		$count = $db_adminlog->join('__ADMIN__ a ON l.adminid = a.heard_admin_id')->count();
		$p = getpage($count, 40);
		$adminloglist = $db_adminlog->join('__ADMIN__ a ON l.adminid = a.heard_admin_id')->order("id desc")->limit($p->firstRow, $p->listRows)->select();
		// p($adminloglist);exit();
		$this->assign('adminloglist',$adminloglist);
		$this->assign('page', $p->show());
		$this->display();
	}


}